package model;

import java.awt.*;
import ds.MyLinkedList;
import ds.MyGraph;
import java.util.List;

public class Map {
    public static final int ROWS = 22;
    public static final int COLS = 20;
    public static final int TILE_SIZE = 40;
    // 0: 路, 1: 墙, 2: 豆子
    private int[][] grid = {
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,1},
        {1,2,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,2,2,1},
        {1,2,1,2,2,2,2,2,1,2,2,2,1,2,2,2,1,2,2,1},
        {1,2,1,1,1,1,1,2,1,1,1,2,1,1,1,1,1,2,2,1},
        {1,2,2,2,2,2,1,2,2,2,2,2,1,2,2,2,2,2,2,1},
        {1,2,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,2,2,1},
        {1,2,2,2,1,2,2,2,1,2,1,2,2,2,1,2,2,2,2,1},
        {1,1,1,2,1,1,1,2,1,2,1,2,1,1,1,2,1,1,1,1},
        {1,2,2,2,2,2,2,2,0,0,0,0,2,2,2,2,2,2,2,1},
        {1,2,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,2,1},
        {1,2,2,2,2,2,2,2,1,0,0,1,2,2,2,2,2,2,2,1},
        {1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,1},
        {1,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,1},
        {1,2,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,2,2,1},
        {1,2,1,2,2,2,2,2,1,2,2,2,1,2,2,2,1,2,2,1},
        {1,2,1,1,1,1,1,2,1,1,1,2,1,1,1,1,1,2,2,1},
        {1,2,2,2,2,2,1,2,2,2,2,2,1,2,2,2,2,2,2,1},
        {1,2,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,2,2,1},
        {1,2,2,2,1,2,2,2,1,2,1,2,2,2,1,2,2,2,2,1},
        {1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    };

    private MyLinkedList dotList = new MyLinkedList();
    private MyGraph graph;

    public Map() {
        buildDotList();
        buildGraph();
    }

    private int nodeId(int row, int col) { return row * COLS + col; }

    private void buildGraph() {
        graph = new MyGraph(ROWS * COLS);
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                if (!isWall(row, col)) {
                    for (int[] d : new int[][]{{-1,0},{1,0},{0,-1},{0,1}}) {
                        int nr = row + d[0], nc = col + d[1];
                        if (!isWall(nr, nc)) {
                            graph.addEdge(nodeId(row, col), nodeId(nr, nc));
                        }
                    }
                }
            }
        }
    }

    public List<Integer> getGraphNeighbors(int row, int col) {
        return graph.getNeighbors(nodeId(row, col));
    }

    // 构建链表
    private void buildDotList() {
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                if (grid[row][col] == 2) {
                    dotList.add(new Dot(row, col));
                }
            }
        }
    }

    // 绘制墙和豆子（豆子用链表遍历）
    public void draw(Graphics g) {
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                int x = col * TILE_SIZE;
                int y = row * TILE_SIZE;
                if (grid[row][col] == 1) {
                    g.setColor(Color.BLUE);
                    g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
                }
            }
        }
        // 画豆子（链表）
        MyLinkedList.Node cur = dotList.getHead();
        while (cur != null) {
            Dot dot = (Dot) cur.value;
            int x = dot.col * TILE_SIZE;
            int y = dot.row * TILE_SIZE;
            g.setColor(Color.WHITE);
            g.fillOval(x + TILE_SIZE/3, y + TILE_SIZE/3, TILE_SIZE/3, TILE_SIZE/3);
            cur = cur.next;
        }
    }

    // Pacman吃豆子时，链表删除节点
    public boolean eatDot(int row, int col) {
        MyLinkedList.Node cur = dotList.getHead();
        while (cur != null) {
            Dot dot = (Dot) cur.value;
            if (dot.row == row && dot.col == col) {
                dotList.remove(dot);
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public int getRows() { return grid.length; }
    public int getCols() { return grid[0].length; }

    public boolean isWall(int row, int col) {
        // 越界也视为墙
        if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return true;
        return grid[row][col] == 1;
    }

    // 获取某点的可达邻居（用于图算法）
    public java.util.List<int[]> getNeighbors(int row, int col) {
        int[] dr = {-1, 1, 0, 0};
        int[] dc = {0, 0, -1, 1};
        java.util.List<int[]> neighbors = new java.util.ArrayList<>();
        for (int d = 0; d < 4; d++) {
            int nr = row + dr[d];
            int nc = col + dc[d];
            if (!isWall(nr, nc)) {
                neighbors.add(new int[]{nr, nc});
            }
        }
        return neighbors;
    }

    public MyGraph getGraph() {
        return graph;
    }
} 